package o;

import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Environment;
import android.text.TextUtils;
import com.huawei.appgallery.game.impl.GameResource;
import com.huawei.appgallery.game.impl.GameResourceInfoRequest;
import com.huawei.appgallery.game.impl.GameResourceInfoRequest.PackageList;
import com.huawei.appgallery.game.impl.GameResourceInfoResponse;
import com.huawei.appgallery.game.impl.GameResourceSwitchReceiver;
import com.huawei.appgallery.game.impl.PackageInfo;
import com.huawei.appgallery.game.impl.PackageUpdateInfo;
import com.huawei.hmf.annotation.ApiDefine;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@ApiDefine(uri = cld.class)
public class clh implements cld {
    private long ˏ = 0;

    public void ˏ() {
        if (cln.ʼ()) {
            cle.ˎ.ˋ("GameResourcePreLoad", "initDB");
            clf.ॱ(new GameResource());
        }
    }

    public void ˊ() {
        if (cln.ʼ()) {
            cle.ˎ.ˋ("GameResourcePreLoad", "init");
            cfw.ˊ(GameResourceInfoRequest.APIMETHOD, GameResourceInfoResponse.class);
            GameResourceSwitchReceiver.register();
        }
    }

    public void ˋ() {
        if (cln.ʼ()) {
            cle.ˎ.ˋ("GameResourcePreLoad", "doJob");
            if (cln.ˎ() == -1) {
                cln.ˏ();
            }
            if (!ᐝ()) {
                cln.ʻ();
            }
            if (ॱ()) {
                ʽ();
            }
            cln.ˊ();
        }
    }

    public void ˎ() {
        if (cln.ʼ()) {
            cle.ˎ.ˋ("GameResourcePreLoad", "checkOldFiles");
            for (GameResource gameResource : clg.ˏ().ॱ()) {
                cln.ˋ(new File(gameResource.ʻ()));
                gameResource.ˏ(5);
                clg.ˏ().ˎ(gameResource);
            }
        }
    }

    private boolean ॱ() {
        boolean z = true;
        boolean z2 = cln.ᐝ() && ᐝ() && eqp.ᐝ() && ʼ();
        if (cln.ॱॱ()) {
            z = z2;
        } else if (!z2 || etp.ˋ()) {
            z = false;
        }
        cle.ˎ.ˋ("GameResourcePreLoad", "shouldStartJob=" + z);
        return z;
    }

    private boolean ʼ() {
        if (VERSION.SDK_INT < 23 || ContentManager.getInstant().getContext().checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == 0) {
            return true;
        }
        return false;
    }

    private boolean ᐝ() {
        fhz ˋ = fho.ˏ().ˋ("UpdateManager");
        if (ˋ != null) {
            return ((czp) ˋ.ॱ(czp.class)).ˎ(ContentManager.getInstant().getContext());
        }
        cle.ˎ.ˏ("GameResourcePreLoad", "no UpdateManager module");
        return false;
    }

    private void ʽ() {
        cle.ˎ.ˋ("GameResourcePreLoad", "startJob");
        List ˋ = cln.ˋ();
        List ˊ = clg.ˏ().ˊ();
        ˎ(ˊ(ˊ(ˋ, ˊ)), ˊ);
    }

    private List<PackageInfo> ˊ(List<String> list, List<GameResource> list2) {
        List<PackageInfo> arrayList = new ArrayList();
        for (String str : list) {
            PackageInfo packageInfo = new PackageInfo();
            arrayList.add(packageInfo);
            packageInfo.ˊ(str);
            GameResource ॱ = ॱ(str, list2);
            if (ॱ == null) {
                packageInfo.ˊ(0);
            } else {
                packageInfo.ˊ(ॱ.ˎ());
            }
        }
        return arrayList;
    }

    private GameResource ॱ(String str, List<GameResource> list) {
        for (GameResource gameResource : list) {
            if (gameResource.ॱ().equals(str)) {
                return gameResource;
            }
        }
        return null;
    }

    private List<PackageUpdateInfo> ˊ(List<PackageInfo> list) {
        if (list.size() <= 2000) {
            return ˋ((List) list);
        }
        List<PackageUpdateInfo> arrayList = new ArrayList();
        List arrayList2 = new ArrayList();
        int i = 0;
        for (PackageInfo add : list) {
            arrayList2.add(add);
            int i2 = i + 1;
            if (i2 == 2000) {
                arrayList.addAll(ˋ(arrayList2));
                arrayList2.clear();
                i2 = 0;
            }
            i = i2;
        }
        if (arrayList2.size() > 0) {
            arrayList.addAll(ˋ(arrayList2));
        }
        return arrayList;
    }

    private List<PackageUpdateInfo> ˋ(List<PackageInfo> list) {
        cle.ˎ.ˋ("GameResourcePreLoad", "getUpdateInfoOnce");
        ArrayList arrayList = new ArrayList();
        if (list.isEmpty()) {
            return arrayList;
        }
        cvt gameResourceInfoRequest = new GameResourceInfoRequest();
        PackageList packageList = new PackageList();
        packageList.ˊ(list);
        gameResourceInfoRequest.ˎ(packageList);
        GameResourceInfoResponse gameResourceInfoResponse = (GameResourceInfoResponse) cfw.ॱ(gameResourceInfoRequest);
        if (gameResourceInfoResponse != null) {
            if (gameResourceInfoResponse.getResponseCode() != 0 || gameResourceInfoResponse.getRtnCode_() != 0) {
                cle.ˎ.ˊ("GameResourcePreLoad", "getUpdateInfoOnce failed. responseCode=" + gameResourceInfoResponse.getResponseCode() + " returnCode=" + gameResourceInfoResponse.getRtnCode_());
            } else if (gameResourceInfoResponse.ˎ() != null && !gameResourceInfoResponse.ˎ().isEmpty()) {
                return gameResourceInfoResponse.ˎ();
            } else {
                cle.ˎ.ˋ("GameResourcePreLoad", "no update");
            }
        }
        return arrayList;
    }

    private void ˎ(List<PackageUpdateInfo> list, List<GameResource> list2) {
        this.ˏ = cln.ʽ();
        cle.ˎ.ˋ("GameResourcePreLoad", "freeSize=" + this.ˏ);
        for (PackageUpdateInfo ˋ : list) {
            ˋ(ˋ, list2);
        }
    }

    private void ˋ(PackageUpdateInfo packageUpdateInfo, List<GameResource> list) {
        GameResource ˎ;
        long ˋ;
        for (GameResource ˎ2 : list) {
            if (ˎ2.ॱ().equals(packageUpdateInfo.ॱ())) {
                if (ˎ2.ॱॱ() == 1) {
                    cle.ˎ.ˋ("GameResourcePreLoad", "remove downloading task:" + ˎ2.ॱ());
                    ˎ(ˎ2.ʼ());
                }
                ˎ2 = ˎ(packageUpdateInfo);
                ˋ = ˋ(ˎ2);
                if (ˋ > 0) {
                    ˎ2.ॱ(ˋ);
                    ˎ2.ˏ(1);
                    clg.ˏ().ˎ(ˎ2);
                    this.ˏ -= ˎ2.ͺ();
                }
            }
        }
        ˎ2 = ˎ(packageUpdateInfo);
        ˋ = ˋ(ˎ2);
        if (ˋ > 0) {
            ˎ2.ॱ(ˋ);
            ˎ2.ˏ(1);
            clg.ˏ().ˎ(ˎ2);
            this.ˏ -= ˎ2.ͺ();
        }
    }

    private void ˎ(long j) {
        ((DownloadManager) ContentManager.getInstant().getContext().getSystemService("download")).remove(new long[]{j});
    }

    private long ˋ(GameResource gameResource) {
        cle.ˎ.ˋ("GameResourcePreLoad", "addTask package=" + gameResource.ॱ());
        if (TextUtils.isEmpty(gameResource.ᐝ())) {
            cle.ˎ.ˏ("GameResourcePreLoad", "downloadUrl is empty");
            return -1;
        }
        cle.ˎ.ˋ("GameResourcePreLoad", "freeSize=" + this.ˏ + ", requiredSize=" + (gameResource.ͺ() * 3));
        if (this.ˏ < gameResource.ͺ() * 3) {
            cle.ˎ.ˏ("GameResourcePreLoad", "not enough space:" + gameResource.ॱ());
            return -1;
        }
        long currentTimeMillis = System.currentTimeMillis();
        clj.ˏ(gameResource.ॱ(), gameResource.ͺ(), gameResource.ˊॱ(), currentTimeMillis);
        String str = gameResource.ॱ() + "_" + currentTimeMillis;
        String ˏ = cln.ˏ(str);
        if (ˋ(ˏ)) {
            gameResource.ˋ(ˏ);
            DownloadManager downloadManager = (DownloadManager) ContentManager.getInstant().getContext().getSystemService("download");
            Request destinationInExternalPublicDir = new Request(Uri.parse(gameResource.ᐝ())).setAllowedNetworkTypes(2).setAllowedOverMetered(false).setRequiresCharging(true).setRequiresDeviceIdle(true).setNotificationVisibility(2).setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, cln.ˎ(str));
            if (cln.ॱॱ()) {
                destinationInExternalPublicDir.setRequiresDeviceIdle(false);
            }
            return downloadManager.enqueue(destinationInExternalPublicDir);
        }
        cle.ˎ.ˋ("GameResourcePreLoad", "mkdirsIfNecessary failed");
        return -1;
    }

    private boolean ˋ(String str) {
        try {
            File parentFile = new File(str).getParentFile();
            if (parentFile == null || parentFile.exists()) {
                return true;
            }
            return parentFile.mkdirs();
        } catch (Exception e) {
            cle.ˎ.ˏ("GameResourcePreLoad", "mkdirsIfNecessary exception");
            return false;
        }
    }

    private GameResource ˎ(PackageUpdateInfo packageUpdateInfo) {
        GameResource gameResource = new GameResource();
        gameResource.ˎ(packageUpdateInfo.ॱ());
        gameResource.ॱ(packageUpdateInfo.ˊ());
        gameResource.ˏ(packageUpdateInfo.ˋ());
        gameResource.ˊ(packageUpdateInfo.ˎ());
        gameResource.ॱ(packageUpdateInfo.ˏ());
        gameResource.ᐝ(packageUpdateInfo.ʼ());
        gameResource.ˊ(packageUpdateInfo.ᐝ());
        return gameResource;
    }
}
